using B.S.Data.MES.API.Applications.BaseData.Command.MaterialGroupCommand;
using B.S.Domain.BasicData.MaterialManagement.MaterialGroups;
using B.S.Domain.BasicData.MaterialManagement.Material_type;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using B.S.Domain.Dto.WuLiaoDto;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.MaterialGroupCommandHandlers
{
    public class GetAllMaterialGroupsWithTypeCommandHandler : IRequestHandler<GetAllMaterialGroupsWithTypeCommand, APIRequst<List<GropDto>>>
    {
        private readonly IBaseRepository<Material_group> _materialGroupRepository;
        private readonly IBaseRepository<Material_type> _materialTypeRepository;

        public GetAllMaterialGroupsWithTypeCommandHandler(
            IBaseRepository<Material_group> materialGroupRepository,
            IBaseRepository<Material_type> materialTypeRepository)
        {
            _materialGroupRepository = materialGroupRepository;
            _materialTypeRepository = materialTypeRepository;
        }

        public Task<APIRequst<List<GropDto>>> Handle(GetAllMaterialGroupsWithTypeCommand request, CancellationToken cancellationToken)
        {
            var list = from mg in _materialGroupRepository.GetAll()
                       join mt in _materialTypeRepository.GetAll() on mg.Materialtypeid equals mt.Materialtypeid
                       select new GropDto
                       {
                           MaterId = mg.MaterId,
                           Materialcode = mg.Materialcode,
                           Materialname = mg.Materialname,
                           Materialtypeid = mg.Materialtypeid,
                           Id = mg.Id,
                           Materialdescription = mg.Materialdescription,
                           MaterialtypeName = mt.MaterialtypeName
                       };

            return Task.FromResult(new APIRequst<List<GropDto>>
            {
                Code = RequstCode.成功,
                Msg = RequstCode.成功.ToString(),
                Data = list.ToList()
            });
        }
    }
}